<!--
 * @Descripttion:
 * @version:
 * @Author: YouHuJun
 * @Date: 2021-09-22 14:51:37
 * @LastEditors: youhujun 2900976495@qq.com
 * @LastEditTime: 2025-05-08 10:56:15
 * @LastEditTime: 2021-09-23 18:51:53
-->
<template>
  <div>
    <el-form
      ref="bindAttributeNameForm"
      :model="bindAttributeNameForm"
      :rules="bindAttributeNameRules"
      label-width="100px"
      :inline="true"
      label-position="left"
      status-icon
      inline-message
      style="width:40%;margin-left:auto;margin-right: auto;padding:10px 10px;"
    >
      <el-row type="flex">
        <el-col :span="24" class="center">
          <el-form-item label="规格名" prop="goods_attribute_name_id">
            <SelectGoodsAttributeName :goods-attribute-name-id.sync="bindAttributeNameForm.goods_attribute_name_id" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="24" class="center">
          <el-form-item>
            <el-button type="primary" @click="submitForm('bindAttributeNameForm')">绑定</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="24" class="center">
          <el-form-item>
            <el-button @click="resetForm('bindAttributeNameForm')">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { debounce } from '@/utils'
import { bindGoodsAttributeName } from '@/api/youhu-shop/V1/business/group/goodsClass/goodsClassUnion/bindAttributeName'

import SelectGoodsAttributeName from '@/pages/component/youhu-shop/v1/select/goods/selectGoodsAttributeName'

import permission from '@/directive/permission/index.js' // 权限判断指令
export default
{
  // 组件名称
  name: 'AddAttributeNameForm',
  // 组件
  components:
    {
      SelectGoodsAttributeName
    },
  directives: { permission },
  props:
    {
      goodsClassId: {
        type: [Number, String],
        default: 0
      }
    },
  // 数据
  data()
  {
    return {
      // 绑定规格名表单
      bindAttributeNameForm: {
        goods_class_id: null,
        goods_attribute_name_id: null
      },
      // 添加表单规则
      bindAttributeNameRules: {
        goods_class_id: [
          { pattern: /^[0-9]+$/, message: '只能是0-9数字', trigger: 'blur' }
        ],
        goods_attribute_name_id: [
          { pattern: /^[0-9]+$/, message: '只能是0-9数字', trigger: 'blur' }
        ]
      }
    }
  },
  // 计算属性
  computed:
  {

  },
  // 监听
  watch:
  {
    goodsClassId: {
      handler(value)
      {
        // console.log('update' + value)

        if (parseInt(value) > 0)
        {
          this.bindAttributeNameForm.goods_class_id = parseInt(value)
        }
      },
      deep: true,
      immediate: true
    }
  },
  // 实例创建之前
  beforeCreate()
  {
  },
  // 创建
  created()
  {

  },
  // 挂载之前
  beforeMount()
  {

  },
  // 挂载 --常用
  mounted()
  {

  },
  // 更新之前
  beforeUpdate()
  {
  },
  // 跟新后
  updated()
  {
  },
  // 销毁之前
  beforeDestroy()
  {
  },
  // 销毁后
  destroyed()
  {
  },
  // 方法
  methods:
  {
    // 初始化表单
    initAttributeNameForm()
    {
      this.bindAttributeNameForm = {
        goods_class_id: 0,
        goods_attribute_name_id: null
      }
    },
    // 提交表单
    submitForm: debounce(function(formName)
    {
      this.$refs[formName].validate((valid) =>
      {
        if (valid)
        {
          // console.log(this.bindAttributeNameForm)
          this.bindAttributeName(this.bindAttributeNameForm)
        }
        else
        {
          console.log('未通过验证!')
          return false
        }
      })
    }, 600),

    // 重置表单
    resetForm(formName)
    {
      this.initAttributeNameForm()
    },

    /**
     * 添加菜单
     */
    bindAttributeName(data)
    {
      const _this = this

      bindGoodsAttributeName(data).then(res =>
      {
        // console.log(res)
        if (res && res.code === 0)
        {
          _this.$emit('handleCloseDialog', false)
          _this.$emit('hanldeUpdateData', true)

          _this.$message(
            {
              message: res.msg,
              type: 'success'
            })
          _this.initAttributeNameForm()
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
 .center{
    text-align: center;
 }
</style>
